package com.example.dnm.controller;

import com.example.dnm.pojo.Menu;
import com.example.dnm.resp.JsonResult;
import com.example.dnm.resp.JsonResultUtil;
import com.example.dnm.service.MenuService;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.association.utility.AppUtility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.temporal.Temporal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @RequestMapping("findAllMenu")
    public JsonResult findAllMenu(Menu menu)
    {
        List<Menu> list=menuService.findAllMenu(menu);
        PageInfo<Menu> menuPageInfo = new PageInfo<>(list);
        return JsonResultUtil.getSuccessJson("查找成功",menuPageInfo);
    }
    @RequestMapping("addMenu")
    public  JsonResult addMenu(Menu menu)
    {
        try {
            Menu menu2=menuService.finMenuById(menu);
            if(menu2!=null)
            {
                return JsonResultUtil.getErrorJson("id已存在");
            }
            if (menu.getMenuid()==null)
            {
                return JsonResultUtil.getErrorJson("菜单名为空");
            }
            Menu menu1=menuService.findMenuByName(menu);
            if (menu1!=null)
            {
                return JsonResultUtil.getErrorJson("菜单名重复");
            }
            menuService.addMenu(menu);
            return JsonResultUtil.getSuccessJson("添加成功");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return JsonResultUtil.getErrorJson("添加失败");
    }
    @RequestMapping("editMenu")
    public JsonResult editMenu(Menu menu)
    {
        try {
            if(menu.getMenuid()==null)
            {
                return JsonResultUtil.getErrorJson("菜单名为空");
            }
            Menu menu1=menuService.findMenuByName(menu);
            if(menu1!=null&!menu1.getId().equals(menu.getId()))
            {
                return JsonResultUtil.getErrorJson("菜单名重复");
            }
            menuService.editMenu(menu);
            return JsonResultUtil.getSuccessJson("更新成功");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return JsonResultUtil.getErrorJson("更新失败");
    }
    @RequestMapping("deleteMenu")
    public  JsonResult deleteMenu(Menu menu)
    {
        try {
            menuService.deleteMenu(menu);
            return  JsonResultUtil.getSuccessJson("删除成功");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return  JsonResultUtil.getErrorJson("删除失败");
    }
}
